package com.ruoyi.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.enums.CollectFileStatusEnum;
import com.ruoyi.dm.domain.*;
import com.ruoyi.dm.domain.po.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 *  po 转 vo 统一工具类
 */
@Slf4j
public class TranferUtils {

    public static AirCollectVO tranferAirRecordPO(AirCollectRecordPO po) {
        AirCollectVO airCollectVO = new AirCollectVO();
        airCollectVO.setId(po.getId());
        airCollectVO.setStatus(CollectFileStatusEnum.statusName(po.getStatus()));
        airCollectVO.setFileName(po.getFileName());
        airCollectVO.setDesc(po.getFileDesc());
        airCollectVO.setUploadTime(po.getUploadTime());
        airCollectVO.setSize(convertFileSize(po.getFileSize()));
        airCollectVO.setSatellite(po.getSatellite());
        airCollectVO.setDateType(StringUtils.hasLength(po.getDataType())?po.getDataType():"-");
        airCollectVO.setTrack(po.getTrack()==null?"-":po.getTrack()+"");
        airCollectVO.setFileDate(po.getFileDate());
        return airCollectVO;
    }

    public static GroundCollectVO tranferGroundRecordPO(GroundCollectRecordPO po) {
        GroundCollectVO vo = new GroundCollectVO();
        vo.setId(po.getId());
        vo.setStatus(CollectFileStatusEnum.statusName(po.getStatus()));
        vo.setFileName(po.getFileName());
        vo.setDesc(po.getFileDesc());
        vo.setUploadTime(po.getUploadTime());
        vo.setSize(convertFileSize(po.getFileSize()));
        vo.setStation(po.getStation());
        vo.setFileDate(po.getFileDate());
        return vo;
    }

    public static ReanalysisVO tranferReanalysisRecordPO(ReanalysisCollectRecordPO po) {
        ReanalysisVO vo = new ReanalysisVO();
        vo.setId(po.getId());
        vo.setStatus(CollectFileStatusEnum.statusName(po.getStatus()));
        vo.setFileName(po.getFileName());
        vo.setDesc(po.getFileDesc());
        vo.setUploadTime(po.getUploadTime());
        vo.setSize(convertFileSize(po.getFileSize()));
        vo.setDataType(po.getDataType());
        vo.setFileDate(po.getFileDate()+"");
        return vo;
    }

    public static EnvCollectVO tranferEnvRecordPO(SpaceEnvCollectRecordPO po) {
        EnvCollectVO vo = new EnvCollectVO();
        vo.setId(po.getId());
        vo.setStatus(CollectFileStatusEnum.statusName(po.getStatus()));
        vo.setFileName(po.getFileName());
        vo.setDesc(po.getFileDesc());
        vo.setUploadTime(po.getUploadTime());
        vo.setSize(convertFileSize(po.getFileSize()));
        vo.setDateType(po.getDataType());
        vo.setFileDate(po.getFileDate());
        return vo;
    }

    public static SpaceTrackDataCollectVO tranferTrackRecordPO(SpaceTrackCollectRecordPO po) {
        SpaceTrackDataCollectVO vo = new SpaceTrackDataCollectVO();
        vo.setId(po.getId());
        vo.setStatus(CollectFileStatusEnum.statusName(po.getStatus()));
        vo.setFileName(po.getFileName());
        vo.setDesc(po.getFileDesc());
        vo.setUploadTime(po.getUploadTime());
        vo.setSize(convertFileSize(po.getFileSize()));
        vo.setSatellite(po.getSatellite());
        vo.setSatelliteCode(po.getSatelliteCode());
        vo.setFileDate(po.getFileDate());
        vo.setUploadTime(po.getUploadTime());
        if (CollectFileTypeConstants.TYPE_SHAPE.equals(po.getDataType())) {
            vo.setDataType("姿态数据");
        }else if (CollectFileTypeConstants.TYPE_TRACK.equals(po.getDataType())) {
            vo.setDataType("轨道数据");
        }
        return vo;
    }


    public static RadioStationVO tranferRadioStationPO(RadioStationPO po) {
        RadioStationVO radioStationVO = new RadioStationVO();
        radioStationVO.setId(po.getId());
        radioStationVO.setLat(po.getLat());
        radioStationVO.setLon(po.getLon());
        radioStationVO.setHight(po.getHight());
        radioStationVO.setDetail(po.getDetail());
        radioStationVO.setName(po.getName());
        radioStationVO.setStartYear(po.getStartYear());
        radioStationVO.setEndYear(po.getEndYear());
        radioStationVO.setTotalDay(po.getTotalDay());
        return radioStationVO;
    }

    public static TrackTleConditonVO tranferTleConditionPO(TrackTleConditonPO po) {
        TrackTleConditonVO vo = new TrackTleConditonVO();
        vo.setId(po.getId());
        vo.setNoradCatId(po.getNoradCatId());
        vo.setObjectName(po.getObjectName());
        vo.setCountry(po.getCountry());
        vo.setPeriod(po.getPeriod());
        vo.setInclination(po.getInclination());
        vo.setApogee(po.getApogee());
        vo.setPerigee(po.getPerigee());
        vo.setRcsSize(po.getRcsSize());
        vo.setLaunch(po.getLaunch());
        vo.setDecay(po.getDecay());
        return vo;
    }

    public static SpaceTargetMeasureVO tranferSpaceTargetMeasurePO(SpaceTargetMeasurePO po) {
        SpaceTargetMeasureVO vo = new SpaceTargetMeasureVO();
        vo.setId(po.getId());
        vo.setCode(po.getCode());
        vo.setName(po.getName());
        vo.setDens(po.getDens());
        vo.setLightIndex(po.getLightIndex());
        vo.setLightArea(po.getLightArea());
        vo.setFIndex(po.getFIndex());
        vo.setWindArea(po.getWindArea());
        vo.setAirDens(po.getAirDens());
        vo.setCreateDate(po.getCreateDate());
        return vo;
    }


    public static NormalFileVO tranferNormalFilePO(NormalFilePO po) {
        NormalFileVO vo = new NormalFileVO();
        vo.setId(po.getId());
        vo.setName(po.getName());
        vo.setType(po.getType());
        vo.setUploadDate(po.getUploadDate());
        vo.setSize(convertFileSize(po.getSize()));
        return vo;
    }


    public static GpsTargetInfoVO tranferGpsTargetInfoPO(GpsTargetInfoPO po) {
        GpsTargetInfoVO vo = new GpsTargetInfoVO();
        vo.setId(po.getId());
        vo.setCode(po.getCode());
        vo.setName(po.getName());
        vo.setQuality(po.getQuality());
        vo.setCreateDate(po.getCreateDate());
        return vo;
    }


    public static TrackTleVO tranferTlePO(CollectTleRecordPO po) {
        TrackTleVO trackTleVO = new TrackTleVO();
        trackTleVO.setCode(po.getTleCode());
        trackTleVO.setLine(po.toString());
        return trackTleVO;
    }

    /**
     * 字节转换
     *
     * @param size 字节大小
     * @return 转换后值
     */
    private static String convertFileSize(long size)
    {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb)
        {
            return String.format("%.1f GB", (float) size / gb);
        }
        else if (size >= mb)
        {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        }
        else if (size >= kb)
        {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        }
        else
        {
            return String.format("%d B", size);
        }
    }

    public static void mai22n(File file, List<File> ls) {
        //低轨 获取目录下文件
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                mai22n(file1,ls);
            }else {
                ls.add(file1);
            }
        }
    }

    public static void main(String[] args) {
        File outFile = new File("D:\\jieya\\champ\\shape");
        ArrayList<File> list = new ArrayList<>();
        mai22n(outFile,list);
        for (File file : list) {
            if (file.getName().endsWith("dif")) {
                file.delete();
            }
        }
    }

    public static void champzitai(String[] args) throws Exception {
        File file = new File("D:\\项目\\低轨大气\\CHAMP-姿态");
        File outFile = new File("D:\\jieya\\champ\\shape");
        ArrayList<File> list = new ArrayList<>();
        mai22n(file,list);
        File ff = null;
        for (File zipFile : list) {
            try {
                String[] split = zipFile.getParentFile().getAbsolutePath().split("\\\\");

                ZipUtil.unzip(zipFile, new File(outFile, split[4]));
            }catch (Exception e) {
                System.out.println(zipFile.getAbsolutePath());
            }
        }
    }

    public static void champTrack() {
        File file = new File("D:\\项目\\低轨大气\\CHAMP-轨道");
        File outFile = new File("D:\\jieya\\champ\\track");
        ArrayList<File> list = new ArrayList<>();
        mai22n(file,list);
        for (File gzFile : list) {
            if (!gzFile.getName().endsWith("zip")) {
                System.out.println(gzFile.getAbsolutePath());
                continue;
            }
            //F:\CHAMP-轨道\2000\202\GFZOP_RSO_L06_G_20000720_100000_20000721_000000_v01.sp3
            String[] split = gzFile.getAbsolutePath().split("\\\\");

            File outputFile = new File(outFile,split[4]+"/"+gzFile.getName().replace(".gz",""));
            outputFile.getParentFile().mkdirs();
            try (InputStream inputStream = new FileInputStream(gzFile);
                 OutputStream outputStream = new FileOutputStream(outputFile);
                 GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {

                byte[] buffer = new byte[10240];
                int len;
                while ((len = gzipInputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                }
            } catch (IOException e) {
                log.info(gzFile.getAbsolutePath());
                e.printStackTrace();
            }
        }
    }


    public static void gnns() throws IOException {
        //解析TGZ文件
        //File file = new File("F:\\精密轨道数据\\GRACEFO\\2021");
        File file = new File("D:\\jieya\\2024");
        String year = "/2024";
        File parentFile = new File("D:\\jieya");
        File ff = null;
        File[] files = file.listFiles();
        for (File file1 : files) {
            TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file1)));
            TarArchiveEntry nextTarEntry = null;
            while ((nextTarEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
                String name = nextTarEntry.getName();
                if (name.endsWith("C_04.txt")) {
                    if (name.startsWith("SCA1B")) {
                        ff = new File(new File(parentFile,"gracefoc"+year),"姿态/"+name);
                    }else if (name.startsWith("GNI1B")) {
                        ff = new File(new File(parentFile,"gracefoc"+year),"轨道/"+name);
                    }
                }else if (name.endsWith("D_04.txt")) {
                    if ( name.startsWith("SCA1B")) {
                        ff = new File(new File(parentFile,"gracefod"+year),"姿态/"+name);
                    }else if (name.startsWith("GNI1B")) {
                        ff = new File(new File(parentFile,"gracefod"+year),"轨道/"+name);
                    }
                }
                if (ff != null) {
                    FileUtil.writeFromStream(tarArchiveInputStream,ff,false);
                }
                ff = null;

            }
            tarArchiveInputStream.close();
            log.info("{} 完成",file1.getName());
        }
    }

}
